﻿@{
    string layerIndex = Request.QueryString["layerIndex"];
    string table = ViewBag.Table;
    string action = ViewContext.RouteData.Values["action"].ToString();
    CurrentUser currentUser = ViewBag.CurrentUser;
    IEnumerable<dynamic> fieldData = Html.GetFieldList((string)ViewBag.Table, "list", currentUser);
}
<div class="current-location hidden">
    <ul>
        <li class="left"><i class="glyphicon glyphicon-home"></i><a href="javascript:void(0)" onclick="location.href=location.href">@ViewBag.TableRemark</a></li>
        <li class="right"><a href="javascript:void(0)" onclick="LoadData()" title="刷新"><span class="glyphicon glyphicon-refresh"></span></a></li>
    </ul>
</div>
<div class="main">
    <table border=0 cellpadding=0 cellspacing=0 class="tb-head form-inline form-group-sm">
        <tr>
            <td>
                <div class="btn-group btn-group-sm">
                    <button type="button" class="btn btn-default ui-selectInvert">反选</button>
                    <div class="btn-group btn-group-sm">
                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                            菜单<span class="caret"></span>
                        </button>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="javascript:void(0)" class="ui-dialog" data-custom-params="title:'添加',url:'@Url.ActionUrl("Add")?table=@table',width:'90%',height:'90%',target:'self'">添加</a></li>
                            <li class="divider"></li>
                            <li><a href="javascript:void(0)" class="ui-updateMultiple" data-custom-params="url:'@Url.ActionUrl("UpdateList")?table=@table',callBack:'LoadData'">更新</a></li>
                            <li class="divider"></li>
                            <li><a href="javascript:void(0)" class="ui-setField" data-custom-params="url:'@Url.ActionUrl("SetField")?table=@table',setField:'isFinal',setValue:'1',limitFinal:'0',includeChildNode:'0',callBack:'LoadData'">转为最终菜单</a></li>
                            <li><a href="javascript:void(0)" class="ui-setField" data-custom-params="url:'@Url.ActionUrl("SetField")?table=@table',setField:'isFinal',setValue:'0',limitFinal:'1',callBack:'LoadData'">转为非最终菜单</a></li>
                            <li class="divider"></li>
                            <li><a href="javascript:void(0)" class="ui-setField" data-custom-params="url:'@Url.ActionUrl("SetField")?table=@table',setField:'state',setValue:'0',callBack:'LoadData'">停用</a></li>
                            <li><a href="javascript:void(0)" class="ui-setField" data-custom-params="url:'@Url.ActionUrl("SetField")?table=@table',setField:'state',setValue:'1',callBack:'LoadData'">启用</a></li>
                            <li class="divider"></li>
                            <li><a href="javascript:void(0)" class="ui-setMultiple" data-custom-params="title:'分类转移',url:'@Url.ActionUrl("Transfer")?table=@table',width:'600px',height:'90%'">转移</a></li>
                            <li class="divider"></li>
                            <li><a href="javascript:void(0)" class="ui-deleteMultiple" data-custom-params="async:false,url:'@Url.ActionUrl("Delete")?table=@table',callBack:'LoadData',reverseExecute:true">删除</a></li>
                        </ul>
                    </div>
                </div>
            </td>
        </tr>
    </table>
    <table border=0 cellpadding=0 cellspacing=0 class="table table-bordered form-inline form-group-sm" id="tb_list">
        <thead>
            <tr>
                <td align="center" style="width:40px"><input type="checkbox" title="选择" class="ui-selectInvert" data-custom-params="isReverse:false"></td>
                @{
                    foreach (var item in fieldData)
                    {
                        if (item.Name == "name")
                        {
                            <td align="left"><span>@item.Remark/</span><span class="ui-folder-switch">展开</span></td>
                        }
                        else
                        {
                            <td align="center">@item.Remark</td>
                        }
                    }
                }
                <td align="center">Id</td>
                <td align="center">启用</td>
                <td align="center">序号</td>
                <td align="center">添加下级</td>
                <td align="center">操作</td>
            </tr>
        </thead>
        <tr class="item list-template-item parent_{parentId}" id='item_{id}' data-id="{id}" data-parentId="{parentId}" data-isFinal="{isFinal}">
            <td align="center"><input type="checkbox" value="{id}" class="checked-item"></td>
            @foreach (var item in fieldData)
            {
                int listWords = item.ListWords;
                string fieldId = item.Id.ToString();
                string field = item.Name;
                string fieldType = item.FieldType;
                string valueType = item.ValueType;
                string align = "center";
                string orderBy = item.Orderby.ToString();
                string pageSize = item.Pagesize.ToString();
                if (valueType == "nvarchar" || valueType == "text")
                {
                    if (fieldType != "image")
                    {
                        align = "left";
                    }
                }

                if (field == "name")
                {
                    <td align="center" style="text-align:left">{space}<span class="node node_{isFinal}"></span><span>{name}</span></td>
                }
                else
                {
                    <td align="@align">
                        @if (new string[] { "images", "files", "subtable" }.Contains(fieldType))
                        {
                            <span class="sp_@field sp_values" data-value-params="id:{id},fieldId:@fieldId,name:'@field',fieldType:'@fieldType',valueType:'@valueType',listWords:'@listWords'"><i class="fa fa-spinner fa-spin"></i></span>
                        }
                        else
                        {
                            <span class="sp_@field sp_values" data-value-params="id:{id},fieldId:@fieldId,name:'@field',fieldType:'@fieldType',valueType:'@valueType',listWords:'@listWords'">{@field}</span>
                        }
                    </td>
                }
            }
                <td align="center">{id}</td>
                <td align="center"><span class="sp_state is-{state}"></span></td>
                <td align="center"><input type="text" name="xuhao" id="xuhao" class="form-control ui-listXuhao" value="{xuhao}" data-table="@table" data-id="{id}" style="width:50px" maxlength="3" /></td>
                <td align="center">
                    <button type="button" data-custom-params="title:'{name}：子分类添加',url:'@Url.ActionUrl("Add")?table=@table&parentId={id}',width:'90%',height:'90%'" class="btn btn-default btn-sm add_chilld_{isFinal} ui-dialog">添加</button>
                </td>
                <td align="center">
                    <input type="hidden" name="id" value="{id}" />
                    <div class="btn-group btn-group-sm">
                        <button type="button" data-custom-params="title:'{name}：编辑',url:'@Url.ActionUrl("Edit")?table=@table&id={id}',width:'90%',height:'90%',target:'self'" class="btn btn-default btn-sm ui-dialog">修改</button>
                        <button type="button" data-custom-params="url:'@Url.ActionUrl("Delete")?table=@table',callBack:'LoadData'" class="btn btn-default btn-sm ui-deleteSingle">删除</button>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <script type="text/javascript">

    function LoadData() {
        var ajaxUrl = "@Url.ActionUrl("ListData")?table=@table";
        $("#tb_list").TreeBind({url:ajaxUrl, callBack: "LoadedInit" });
    }

    function LoadedInit() {

        var $sp_values = $(".sp_values");
        $sp_values.each(function () {
            var $this = $(this);
            var thisHtml = $this.html();
            var params = $this.attr("data-value-params")
            params = ExtendObject(params);
            var parentId = params.id;
            var listWords = parseInt(params.listWords);
            var valueType = params.valueType;
            var fieldType = params.fieldType;
            var field = params.name;
            var fieldId = params.fieldId;
            if (fieldType == "subtable") {
                $.get("/E/SubData/Count", { _fieldId: fieldId, parentId: parentId }, function (data) { $this.text(data) });
            }
            else if (fieldType == "images" || fieldType == "files") {
                $.get("/E/AttachmentsData/Count", { _fieldId: fieldId, parentId: parentId }, function (data) { $this.text(data) });
            }
            else if (params.fieldType == "image" && thisHtml != "") {
                $this.html("<a href=\"" + thisHtml + "\" target=\"_blank\"><img src=\"" + thisHtml + "\" class=\"ui-thumbnail thumbnail\"></a>");
            }
            else if (valueType == "nvarchar" || valueType == "ntext" || valueType == "datetime") {
                if (thisHtml.length > listWords) {
                    if (field == "title") {
                        $this.html(thisHtml.Left(listWords) + "...");
                    }
                    else {
                        $this.html(thisHtml.Left(listWords));
                    }
                }
            }
        });
        $(".add_chilld_0").removeAttr("disabled");
        $(".add_chilld_1").attr("disabled", "disabled");
    }
    LoadData()


    //设置前检测
    function BeforeExecute(ids, setName) {
        var Aids = ids.split(",");
        var currentObj, guid, childObj;
        switch (setName) {
            case "final":
                for (i = 0; i < Aids.length; i++) {
                    currentObj = $("#item_" + Aids[i]);
                    guid = currentObj.attr("guid");
                    childObj = $("tr[parent_guid='" + guid + "']");
                    if (childObj.length > 0) {
                        Alert("选中的分类不能有下级分类!");
                        return false;
                    }
                }
                break;

            case "transfer":
                for (i = 0; i < Aids.length; i++) {
                    currentObj = $("#item_" + Aids[i]);
                    if (currentObj.attr("is_final") == "0") {
                        Alert("只能对最终分类进行转移!");
                        return false;
                    }
                }
                var params = { title: '分类转移', url: '&ids=' + ids + '&treelistcookie=tree_@table', width: '800px', height: '580px', target: 'parent' };
                IDialog(params);
                return false;
                break;
        }
        return true;
    }
    </script>